﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;

namespace Daneas.Utility.Universals
{
    /// <summary>
    /// 缓存相关的操作类
    /// Copyright (C) Maticsoft
    /// </summary>
    public class DataCache
    {
        private static int overDateHour = 2;//缓存过期时间

        /// <summary>
        /// 生成键值
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">对象唯一值</param>
        /// <param name="start">起</param>
        /// <param name="end">止</param>
        /// <param name="rowNum">行号</param>
        /// <returns>键值</returns>
        public static string CachKey<T>(string key, int start, int end, int rowNum)
        {
            string model = typeof(T).Name;

            if (string.IsNullOrEmpty(key))
            {
                key = "0";
            }
            if (start <= 0)
            {
                start = 0;
            }
            if (end <= 0)
            {
                end = 0;
            }
            if (rowNum <= 0)
            {
                rowNum = 0;
            }

            return string.Format("{0}-{1}-{2}-{3}-{4}", model, key, start, end, rowNum);
        }

        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetCache(string key)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[key];
        }

        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static T GetCache<T>(string key)
        {
            string model = typeof(T).Name;
            if (string.IsNullOrEmpty(key))
            {
                key = "0";
            }
            string cacheKey = string.Format("{0}-{1}", model, key);

            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return (T)objCache[key];
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            if (objObject == null)
            {
                return;
            }

            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache<T>(string key, object objObject)
        {
            if (objObject == null)
            {
                return;
            }
            string model = typeof(T).Name;
            if (string.IsNullOrEmpty(key))
            {
                key = "0";
            }
            string cacheKey = string.Format("{0}-{1}", model, key);

            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objObject ,null, DateTime.UtcNow.AddHours(overDateHour),new TimeSpan(overDateHour));
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="slidingExpiration">多久后失效</param>
        public static void SetCache(string CacheKey, object objObject, TimeSpan slidingExpiration)
        {
            if (objObject == null)
            {
                return;
            }
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;

            int hour = slidingExpiration.Hours;

            objCache.Insert(CacheKey, objObject, null, DateTime.Now.AddHours(hour), TimeSpan.Zero);
        }

        /// <summary>
        /// 设置以缓存依赖的方式缓存数据
        /// </summary>
        /// <param name="CacheKey">索引键值</param>
        /// <param name="objObject">缓存对象</param>
        /// <param name="cacheDepen">依赖对象</param>
        /// <param name="dep">依赖项</param>
        public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep)
        {
            if (objObject == null)
            {
                return;
            }
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(
                CacheKey,
                objObject,
                dep,
                System.Web.Caching.Cache.NoAbsoluteExpiration, //从不过期
                System.Web.Caching.Cache.NoSlidingExpiration, //禁用可调过期
                System.Web.Caching.CacheItemPriority.Default,
                null);
        }

        /// <summary>
        /// 更改某个缓存
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void ChangeCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(CacheKey);
            objCache.Insert(CacheKey, objObject);
        }

        public static void RemoveCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(CacheKey);
        }

        public static void RemoveCache<T>(string key)
        {
            string model = typeof(T).Name;
            if (string.IsNullOrEmpty(key))
            {
                key = "0";
            }
            string cacheKey = string.Format("{0}-{1}", model, key);
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(cacheKey);
        }

        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            try
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
                ArrayList al = new ArrayList();
                while (CacheEnum.MoveNext())
                {
                    al.Add(CacheEnum.Key);
                }
                foreach (string key in al)
                {
                    _cache.Remove(key);
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 移除某个键值类型的所有缓存
        /// </summary>
        /// <param name="kind">类型名称</param>
        public static void RemoveKindCache(string kind)
        {
            try
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
                ArrayList al = new ArrayList();

                while (CacheEnum.MoveNext())
                {
                    if (CacheEnum.Key.ToString().Contains(kind))
                    {
                        al.Add(CacheEnum.Key);
                    }
                }
                foreach (string key in al)
                {
                    _cache.Remove(key);
                }
            }
            catch { }
        }
    }

}
